/*
 *
 *  * Copyright (C) 2014 Antonio Leiva Gordillo.
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *
 */

package com.shijun.im.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.shijun.im.activity.activity.LoginView;
import com.shijun.im.activity.presenter.LoginPresenter;
import com.shijun.im.activity.presenter.impl.LoginPresenterImpl;
import com.shijun.im.R;

public class LoginActivity extends Activity implements LoginView, View.OnClickListener {

    private ProgressBar progressBar;
    private EditText username;
    private EditText password;
    private LoginPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        progressBar = (ProgressBar) findViewById(R.id.activity_login_main_progress_pro);
        username = (EditText) findViewById(R.id.activity_login_main_username_ed);
        password = (EditText) findViewById(R.id.activity_login_main_password_ed);
        findViewById(R.id.activity_login_main_login_but).setOnClickListener(this);
        findViewById(R.id.activity_login_main_regist_but).setOnClickListener(this);
        findViewById(R.id.activity_login_main_forgotpassword_but).setOnClickListener(this);

        presenter = new LoginPresenterImpl(this);
    }

    @Override
    protected void onDestroy() {
        presenter.onDestroy();
        super.onDestroy();
    }

    @Override
    public void showProgress() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                progressBar.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void hideProgress() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                progressBar.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void setUsernameError() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                username.setError(getString(R.string.username_error));
            }
        });
    }

    @Override
    public void setPasswordError() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                password.setError(getString(R.string.password_error));
            }
        });
    }

    @Override
    public void navigateToHome() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Log.i("TAG", "登录成功！");
                hideProgress();
                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                finish();
            }
        });
    }

    @Override
    public void showToast(String content) {
        Toast.makeText(this, content, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void loginError() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(LoginActivity.this, "用户名或密码错误！", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.activity_login_main_login_but:
                presenter.validateCredentials(this, username.getText().toString(), password.getText().toString());
                break;
            case R.id.activity_login_main_regist_but:
                Intent intent = new Intent();
                intent.setClass(this, RegistActivity.class);
                startActivity(intent);
                break;
        }
    }
}
